package com.simple.util.math.function;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;

/**
 * @program: simple_tools
 * @description: 数学(二次函数)
 * @author: ChenWenLong
 * @create: 2019-10-24 15:20
 **/
public class QuadraticFunction {

    // y=ax^2 + bx + c
    // 自变量: independent 因变量: dependent
    private BigDecimal a = BigDecimal.ONE;
    private BigDecimal b = BigDecimal.ZERO;
    private BigDecimal c = BigDecimal.ZERO;
    private static QuadraticFunction instance;
    //抛物线上的点
    private List<Point> points = Collections.EMPTY_LIST;
    private QuadraticFunction(){}

    static {
        if(instance == null){
            synchronized (QuadraticFunction.class){
                if(instance == null){
                    instance = new QuadraticFunction();
                }
            }
        }
    }

    /**
     * 功能描述:
     * 〈初始化二次函数〉
     *
     * @params : [a, b, c]
     * @return : void
     * @author : cwl
     * @date : 2019/10/24 15:32
     */
    public static void init(double a, double b,double c){
        if(a == 0){
            throw new RuntimeException("param a is not be zero");
        }
        instance.setA(new BigDecimal(a));
        instance.setB(new BigDecimal(b));
        instance.setC(new BigDecimal(c));
    }

    /**
     * 功能描述:
     * 〈初始化二次函数〉
     *
     * @params : [a, b]
     * @return : void
     * @author : cwl
     * @date : 2019/10/24 15:33
     */
    public static void init(double a, double b){
        if(a == 0){
            throw new RuntimeException("param a is not be zero");
        }
        instance.setA(new BigDecimal(a));
        instance.setB(new BigDecimal(b));
    }

    /**
     * 功能描述:
     * 〈初始化二次函数〉
     *
     * @params : [a]
     * @return : void
     * @author : cwl
     * @date : 2019/10/24 15:34
     */
    public static void init(double a){
        if(a == 0){
            throw new RuntimeException("param a is not be zero");
        }
        instance.setA(new BigDecimal(a));
    }

    /**
     * 功能描述:
     * 〈判断点是否在抛物线上〉
     *
     * @params : [x, y]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/24 15:50
     */
    public static boolean isOnline(double x,double y){
        BigDecimal dependent = new BigDecimal(y);
        BigDecimal square = new BigDecimal(Math.pow(2, x));
        BigDecimal independent = new BigDecimal(x);
        return dependent.equals(square.multiply(instance.getA()).add(independent.multiply(instance.getB())).add(instance.getC()));
    }

    //-b/2a,(4ac-b^2)/4a
    //AxisOfSymmetry
    /**
     * 功能描述:
     * 〈获得对称轴 -b/2a〉
     *
     * @params : []
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/10/24 16:22
     */
    public static BigDecimal getAxisOfSymmetry(){
        return instance.getB().divide(instance.getA().multiply(new BigDecimal(2)),2, RoundingMode.HALF_UP).negate();
    }

    /**
     * 功能描述:
     * 〈获得抛物线的顶点 (4ac-b^2)/4a〉
     *
     * @params : []
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/10/24 16:28
     */
    public static BigDecimal getMaxValue(){
        BigDecimal a = instance.getA();
        BigDecimal b = instance.getB();
        BigDecimal c = instance.getC();
        BigDecimal subtract = new BigDecimal(4).multiply(a).multiply(c).subtract(new BigDecimal(Math.pow(2, b.doubleValue())));
        return subtract.divide(new BigDecimal(4).multiply(a),2,RoundingMode.HALF_UP);
    }

    /**
     * 功能描述:
     * 〈获得抛物线顶点〉
     *
     * @params : []
     * @return : java.util.Map
     * @author : cwl
     * @date : 2019/10/24 16:45
     */
    public static List<Point> getTopPoint(){
        Point point = new Point();
        BigDecimal axisOfSymmetry = getAxisOfSymmetry();
        BigDecimal maxValue = getMaxValue();
        point.setX(axisOfSymmetry);
        point.setY(maxValue);
        List<Point> points = instance.getPoints();
        points.add(point);
        return points;
    }

    //PointOfIntersection
    //-b +- 根号b^2 -4ac / 2a
    /**
     * 功能描述:
     * 〈获得与X轴的交点〉
     *
     * @params : []
     * @return : java.util.List<com.simple.util.math.function.QuadraticFunction.Point>
     * @author : cwl
     * @date : 2019/10/24 16:55
     */
    public static List<Point> getPointOfIntersectionX(){
        BigDecimal bSquare = new BigDecimal(Math.pow(2, instance.getB().doubleValue())).
                setScale(2, RoundingMode.HALF_UP).
                subtract(new BigDecimal(4).multiply(instance.getA()).multiply(instance.getC()));
        BigDecimal result = new BigDecimal(Math.sqrt(bSquare.doubleValue()));
        BigDecimal x1 = instance.getB().negate().add(result).divide(new BigDecimal(2).multiply(instance.getA()), 2, RoundingMode.HALF_UP);
        BigDecimal x2 = instance.getB().negate().subtract(result).divide(new BigDecimal(2).multiply(instance.getA()), 2, RoundingMode.HALF_UP);
        Point point1 = new Point();
        point1.setX(x1);
        point1.setY(BigDecimal.ZERO);
        Point point2 = new Point();
        point2.setY(x2);
        point2.setY(BigDecimal.ZERO);
        instance.setPoints(Collections.EMPTY_LIST);
        List<Point> points = instance.getPoints();
        points.add(point1);
        points.add(point2);
        return points;
    }

    //抛物线上的点
    private static class Point{
        // x坐标
        private BigDecimal x;
        // y坐标
        private BigDecimal y;

        public BigDecimal getX() {
            return x;
        }

        public void setX(BigDecimal x) {
            this.x = x;
        }

        public BigDecimal getY() {
            return y;
        }

        public void setY(BigDecimal y) {
            this.y = y;
        }
    }

    public List<Point> getPoints() {
        return points;
    }

    public void setPoints(List<Point> points) {
        this.points = points;
    }

    private BigDecimal getA() {
        return a;
    }

    private void setA(BigDecimal a) {
        this.a = a;
    }

    private BigDecimal getB() {
        return b;
    }

    private void setB(BigDecimal b) {
        this.b = b;
    }

    private BigDecimal getC() {
        return c;
    }

    private void setC(BigDecimal c) {
        this.c = c;
    }
}
